<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="favicon.ico">

    <title>在线排序</title>

    <!-- Bootstrap core CSS -->
    <link href="../bootstrap-4.0.0-beta/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="../bootstrap-4.0.0-beta/docs/4.0/examples/starter-template/starter-template.css" rel="stylesheet">
</head>

<body>

<!--<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarsExampleDefault">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link</a>
            </li>
            <li class="nav-item">
                <a class="nav-link disabled" href="#">Disabled</a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="http://example.com" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
                <div class="dropdown-menu" aria-labelledby="dropdown01">
                    <a class="dropdown-item" href="#">Action</a>
                    <a class="dropdown-item" href="#">Another action</a>
                    <a class="dropdown-item" href="#">Something else here</a>
                </div>
            </li>
        </ul>
        <form class="form-inline my-2 my-lg-0">
            <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
            <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
        </form>
    </div>
</nav>-->

<div class="container">
    <div class="starter-template">
        <h1>在线排序</h1>
        <p></p>
        <button class="btn btn-info" type="button" style="width: 100px;" onclick="showYou()">用法展示</button>
        <button class="btn btn-success" type="button" style="width: 100px;" onclick="clearAll()">清空</button>
        <!--<p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>-->
    </div>

    <div class="row center">
        <div class="col-4">
            <ul id="standardTabs" class="nav nav-tabs">
                <li class="nav-item">
                    <a class="nav-link active" href="#standardDiv">排序标准A <span id="standardBadge" class="badge badge-info">0</span></a>
                </li>
            </ul>
        </div>
        <div class="col-4">
            <ul id="dataTabs" class="nav nav-tabs">
                <li class="nav-item">
                    <a class="nav-link active" href="#dataDiv">要排的数据B <span id="dataBadge" class="badge badge-info">0</span></a>
                </li>
            </ul>
        </div>
        <div class="col-4">
            <ul id="myTabs" class="nav nav-tabs">
                <li class="nav-item">
                    <a class="nav-link active" href="#aSortB">排序 <span id="sortBadge" class="badge badge-info">0</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#aMinusB">A-B <span id="aMinusBBadge" class="badge badge-info">0</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#bMinusA">B-A <span id="bMinusABadge" class="badge badge-info">0</span></a>
                </li>
                <!--<li class="nav-item">
                    <a class="nav-link" href="#aPlusB">A+B</a>
                </li>-->
            </ul>
        </div>
    </div>
    <div class="row">
        <div class="col-4">
            <div id="standardDiv" class="tab-pane in active" aria-labelledby="home-tab">
                <textarea id="standard" style="height: 500px;width: 100%;" onchange="goSort()"></textarea>
            </div>
        </div>
        <div class="col-4">
            <div id="dataDiv" class="tab-pane in active" aria-labelledby="home-tab">
                <textarea id="data" style="height: 500px;width: 100%;" onchange="goSort()"></textarea>
            </div>
        </div>
        <div class="col-4">
            <div class="tab-content">
                <div id="aSortB" class="tab-pane in active" aria-labelledby="home-tab">
                    <textarea id="resultSort" style="height: 500px;width: 100%;"></textarea>
                </div>
                <div id="aMinusB" class="tab-pane fade"><textarea id="resultAMinusB" style="height: 500px;width: 100%;" onchange="goSort()"></textarea></div>
                <div id="bMinusA" class="tab-pane fade"><textarea id="resultBMinusA" style="height: 500px;width: 100%;" onchange="goSort()"></textarea></div>
                <!--<div id="aPlusB" class="tab-pane fade"><textarea id="resultA+B" style="height: 500px;width: 100%;" onchange="goSort()"></textarea></div>-->
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-12">
            <div class="alert alert-warning alert-dismissible" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
                <p>1.排序标准A为一列数据，尾部换行与空格将被忽略</p>
                <p>2.要排序的数据B可以为多列，如果多列需要用空格或者tab符分割，第一列作为排序依据</p>
            </div>
        </div>
    </div>
</div>


<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script>window.jQuery || document.write('<script src="../../../../assets/js/vendor/jquery.min.js"><\/script>')</script>
<script src="../bootstrap-4.0.0-beta/assets/js/vendor/popper.min.js"></script>
<script src="../bootstrap-4.0.0-beta/dist/js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../bootstrap-4.0.0-beta/assets/js/ie10-viewport-bug-workaround.js"></script>
<script>
    $(document).ready(function () {
        $('#myTabs a').click(function (e) {
            e.preventDefault();
            $(this).tab('show');
            var href = $(this).attr("href");
        })
    });

    function goSort() {
        var standard = $("#standard").val();
        var data = $("#data").val();
        if (standard) {
            var standardArr = standard.split("\n");
            var standardArrTrim = [];
            var standardEmptyCount = 0;
            for (var i = standardArr.length; i > 0; i--) {
                if (standardArr[i - 1].trim() == "") {
                    standardEmptyCount++;
                } else {
                    break;
                }
            }
            $("#standardBadge").html(standardArr.length - standardEmptyCount);
            $.each(standardArr, function (i, item) {
                if (i < standardArr.length - standardEmptyCount) {
                    standardArrTrim.push(item.trim());
                }
            });
            if (data) {
                var dataArr = data.split("\n");
                var dataEmptyCount = 0;
                var dataDict = [];
                for (var i = dataArr.length; i > 0; i--) {
                    if (dataArr[i - 1].trim() == "") {
                        dataEmptyCount++;
                    } else {
                        break;
                    }
                }
                $("#dataBadge").html(dataArr.length - dataEmptyCount);
                $.each(dataArr, function (i, item) {
                    if (i < dataArr.length - dataEmptyCount) {
                        var itemArr = item.split(" ");
                        //如果用空格分割无效,尝试用tab分割
                        if (itemArr == item) {
                            itemArr = item.split("\t");
                        }
                        dataDict[itemArr[0]] = item;
                    }
                });
            }
            if (standard && data) {
                var result = "";
                var resultArr = [];
                var resultCount = 0;
                var resultAMinusB = "";
                var resultAMinusBCount = 0;

                $.each(standardArrTrim, function (i, item) {
                    if (dataDict[item]) {
                        result += dataDict[item] + "\n";
                        resultArr[item] = true;
                        resultCount++;
                    } else {
                        result += "\n";
                        resultAMinusB += item + "\n";
                        resultAMinusBCount++;
                    }
                });

                var resultBMinusA = "";
                var resultBMinusACount = 0;
                $.each(dataArr, function (i, item) {
                    if (i < dataArr.length - dataEmptyCount) {
                        var itemArr = item.split(" ");
                        //如果用空格分割无效,尝试用tab分割
                        if (itemArr == item) {
                            itemArr = item.split("\t");
                        }
                        if (!resultArr[itemArr[0]]) {
                            resultBMinusA += item + "\n";
                            resultBMinusACount++;
                        }
                    }
                });

                $("#sortBadge").html(resultCount);
                $("#aMinusBBadge").html(resultAMinusBCount);
                $("#bMinusABadge").html(resultBMinusACount);
                $("#resultSort").val(result);
                $("#resultAMinusB").val(resultAMinusB);
                $("#resultBMinusA").val(resultBMinusA);
            }
        }
    }
    
    function showYou(){
        $("#standard").val("张三\n李四\n王五\n赵六");
        $("#data").val("王五 99\n张三 88\n赵六 98\n孙七 66");
        goSort();
    }

    function clearAll(){
        $("#standard").val("");
        $("#data").val("");
        $("#resultSort").val("");
        $("#resultAMinusB").val("");
        $("#resultBMinusA").val("");
        $(".badge").html("0");
    }
</script>
</body>
</html>
